<template>
  <div class="ma-content-block">
    <sa-table ref="crudRef" :options="options" :columns="columns" :searchForm="searchForm">
      <!-- 搜索区 tableSearch -->
      <template #tableSearch>
        <a-col :span="8">
          <a-form-item label="UID" field="id">
            <a-input v-model="searchForm.id" placeholder="请输入UID" />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="用户名" field="username">
            <a-input v-model="searchForm.username" placeholder="请输入用户名" />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="昵称" field="nickname">
            <a-input v-model="searchForm.nickname" placeholder="请输入昵称" />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="微信开发 ID" field="openid">
            <a-input v-model="searchForm.openid" placeholder="请输入微信开发 ID" />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="会员到期时间" field="level_endtime">
            <a-range-picker v-model="searchForm.level_endtime" :show-time="true" mode="date" />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="注册 IP" field="reg_ip">
            <a-input v-model="searchForm.reg_ip" placeholder="请输入注册 IP" />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="最后登录时间" field="last_login_time">
            <a-range-picker v-model="searchForm.last_login_time" :show-time="true" mode="date" />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="创建时间" field="create_time">
            <a-range-picker v-model="searchForm.create_time" :show-time="true" mode="date" />
          </a-form-item>
        </a-col>
      </template>

      <!-- Table 自定义渲染 -->
      <template #status="{ record }">
        <sa-switch v-model="record.status" @change="changeStatus($event, record.id)"></sa-switch>
      </template>
    </sa-table>

    <!-- 编辑表单 -->
    <edit-form ref="editRef" @success="refresh" />
  </div>
</template>

<script setup>
import { onMounted, ref, reactive } from 'vue'
import { Message } from '@arco-design/web-vue'
import commonApi from '@/api/common'
import EditForm from './edit.vue'
import api from '../api/user'

// 引用定义
const crudRef = ref()
const editRef = ref()
const optionData = reactive({
  level: [],
})

// 搜索表单
const searchForm = ref({
  id: '',
  username: '',
  nickname: '',
  openid: '',
  level_endtime: [],
  reg_ip: '',
  last_login_time: [],
  create_time: [],
})

const changeStatus = async (status, id) => {
  const response = await api.changeStatus({ id, status })
  if (response.code === 200) {
    Message.success(response.message)
    crudRef.value.refresh()
  }
}

// SaTable 基础配置
const options = reactive({
  api: api.getPageList,
  recycleApi: api.getRecyclePageList,
  rowSelection: { showCheckedAll: true },
  add: {
    show: true,
    auth: ['/ai/User/save'],
    func: async () => {
      editRef.value?.open()
    },
  },
  edit: {
    show: true,
    auth: ['/ai/User/update'],
    func: async (record) => {
      editRef.value?.open('edit')
      editRef.value?.setFormData(record)
    },
  },
  delete: {
    show: true,
    auth: ['/ai/User/destroy'],
    func: async (params) => {
      const resp = await api.delete(params)
      if (resp.code === 200) {
        Message.success(`删除成功！`)
        crudRef.value?.refresh()
      }
    },
    realAuth: ['/ai/User/realDestroy'],
    realFunc: async (params) => {
      const resp = await api.realDestroy(params)
      if (resp.code === 200) {
        Message.success(`销毁成功！`)
        crudRef.value?.refresh()
      }
    },
  },
  recovery: {
    show: true,
    auth: ['/ai/User/recovery'],
    func: async (params) => {
      const resp = await api.recovery(params)
      if (resp.code === 200) {
        Message.success(`恢复成功！`)
        crudRef.value?.refresh()
      }
    },
  },
})

// SaTable 列配置
const columns = reactive([
  { title: 'UID', dataIndex: 'id', width: 180, sortable: { sortDirections: ['ascend', 'descend'] } },
  { title: '用户名', dataIndex: 'username', width: 180 },
  { title: '昵称', dataIndex: 'nickname', width: 180 },
  { title: '密码', dataIndex: 'password', width: 180 },
  { title: '微信开发 ID', dataIndex: 'openid', width: 180 },
  { title: '头像', dataIndex: 'avatar', type: 'image', width: 180 },
  { title: '金币余额', dataIndex: 'coin_balance', width: 180 },
  { title: '钻石余额', dataIndex: 'diamond_balance', width: 180 },
  { title: '启用状态', dataIndex: 'status', dict: 'yes_or_no', width: 180 },
  { title: '用户等级', dataIndex: 'level', width: 180 },
  { title: '折扣', dataIndex: 'discount', width: 180 },
  { title: '会员到期时间', dataIndex: 'level_endtime', width: 180 },
  { title: '注册 IP', dataIndex: 'reg_ip', width: 180 },
  { title: '最后登录时间', dataIndex: 'last_login_time', width: 180 },
  { title: '创建时间', dataIndex: 'create_time', width: 180 },
  { title: '更新时间', dataIndex: 'update_time', width: 180 },
])

// 页面数据初始化
const initPage = async () => {
  const resp_level = await commonApi.commonGet('/ai/Level/index?saiType=all')
  optionData.level = resp_level.data
}

// SaTable 数据请求
const refresh = async () => {
  crudRef.value?.refresh()
}

// 页面加载完成执行
onMounted(async () => {
  initPage()
  refresh()
})
</script>
